package com.hibase.core.i18n.controller;

import com.github.pagehelper.PageInfo;
import com.hibase.common.entity.ResponseModel;
import com.hibase.common.util.EntityUtil;
import com.hibase.core.i18n.entity.MdsMessageSource;
import com.hibase.core.i18n.entity.vo.*;
import com.hibase.core.i18n.service.MdsMessageSourceService;
import com.hibase.core.web.annotation.HibasePostRequestMapping;
import com.hibase.core.web.entity.OperationIdEntity;
import com.hibase.core.web.entity.OperationIdsEntity;
import com.hibase.common.exception.HibaseAssert;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 国际化信息表 前端控制器
 * </p>
 *
 * @author chenfeng
 * @since 2021-03-24
 */
@RestController
@RequestMapping("/mds-message-source")
@Api(tags = "国际化信息表")
public class MdsMessageSourceController {

    @Autowired
    private MdsMessageSourceService mdsMessageSourceService;


    @ApiOperation(value = "查询国际化信息表列表", httpMethod = "POST")
    @HibasePostRequestMapping(value = "selectMdsMessageSourceList")
    public ResponseModel<MdsMessageSourceListVO> selectMdsMessageSourceList(@RequestBody MdsMessageSourceSearchVO searchVO) {

        PageInfo result = mdsMessageSourceService.selectMdsMessageSourceList(searchVO);

        result.setList(EntityUtil.transform(result.getList(), MdsMessageSourceListVO.class));

        return ResponseModel.successMsg(result);
    }


    @ApiOperation(value = "新增国际化信息表记录", httpMethod = "POST")
    @HibasePostRequestMapping(value = "insertMdsMessageSource")
    public ResponseModel<String> insertMdsMessageSource(@RequestBody MdsMessageSourceInsertVO insertVO) {

        MdsMessageSource entity = EntityUtil.transform(insertVO, MdsMessageSource.class);

        mdsMessageSourceService.saveMdsMessageSource(entity);

        return ResponseModel.successMsg(entity.getId());
    }


    @ApiOperation(value = "批量新增国际化信息表记录", httpMethod = "POST")
    @HibasePostRequestMapping(value = "insertMdsMessageSourceBatch")
    public ResponseModel<String> insertMdsMessageSourceBatch(@RequestBody MdsMessageSourceInsertBatchVO insertVO) {
        List<String> ids = new ArrayList<>();
        insertVO.getAddLangArr().forEach(insert ->{
            MdsMessageSource entity = EntityUtil.transform(insert, MdsMessageSource.class);
            mdsMessageSourceService.saveMdsMessageSource(entity);
            ids.add(entity.getId());
        });
        return ResponseModel.successMsg(ids.stream().collect(Collectors.joining()), ",");
    }


    @ApiOperation(value = "修改国际化信息表记录", httpMethod = "POST")
    @HibasePostRequestMapping(value = "updateMdsMessageSource")
    public ResponseModel<String> updateMdsMessageSource(@RequestBody MdsMessageSourceUpdateVO updateVO) {

        MdsMessageSource entity = EntityUtil.transform(updateVO, MdsMessageSource.class);

        mdsMessageSourceService.saveMdsMessageSource(entity);

        return ResponseModel.successMsg(entity.getId());
    }


    @ApiOperation(value = "批量修改国际化信息表记录", httpMethod = "POST")
    @HibasePostRequestMapping(value = "updateMdsMessageSourceBatch")
    public ResponseModel<String> updateMdsMessageSourceBatch(@RequestBody MdsMessageSourceUpdateBatchVO updateVO) {

        updateVO.getEditLangArr().stream().forEach(update ->{
            MdsMessageSource entity = EntityUtil.transform(update, MdsMessageSource.class);
            mdsMessageSourceService.saveMdsMessageSource(entity);
        });
        return ResponseModel.successMsg(updateVO.getEditLangArr().stream().map(MdsMessageSourceUpdateVO::getId).collect(Collectors.joining()),",");
    }


    @ApiOperation(value = "查询国际化信息表详情", httpMethod = "POST")
    @HibasePostRequestMapping(value = "viewMdsMessageSourceDetails")
    public ResponseModel<MdsMessageSourceViewVO> viewMdsMessageSourceDetails(@RequestBody OperationIdEntity operationidEntity) {

        return ResponseModel.successMsg(EntityUtil.transform(mdsMessageSourceService.viewMdsMessageSource(operationidEntity.getId()), MdsMessageSourceViewVO.class));
    }


    @ApiOperation(value = "删除国际化信息表记录", httpMethod = "POST")
    @HibasePostRequestMapping(value = "/deleteMdsMessageSource")
    public ResponseModel<String> deleteMdsMessageSource(@RequestBody OperationIdEntity delete) {

        mdsMessageSourceService.deleteMdsMessageSource(delete.getId());

        return ResponseModel.successMsg();
    }
    
    @ApiOperation(value = "查询国际化信息表列表", httpMethod = "POST")
    @HibasePostRequestMapping(value = "selectList")
    public ResponseModel<Map<String, String>> selectList(@RequestBody MdsMessageSourceSearchVO searchVO) {

        Map<String, String> map = mdsMessageSourceService.selectList(searchVO);
        return ResponseModel.successMsg(map);
    }


    @ApiOperation(value = "启用国际化信息表", httpMethod = "POST")
    @HibasePostRequestMapping(value = "/enableMdsMessageSource")
    public ResponseModel<String> enableMdsMessageSource(@RequestBody OperationIdsEntity ids) {

        mdsMessageSourceService.enableMdsMessageSource(ids.getIds());

        return ResponseModel.successMsg();
    }


    @ApiOperation(value = "禁用国际化信息表", httpMethod = "POST")
    @HibasePostRequestMapping(value = "/disableMdsMessageSource")
    public ResponseModel<String> disableMdsMessageSource(@RequestBody OperationIdsEntity ids) {

        mdsMessageSourceService.disableMdsMessageSource(ids.getIds());

        return ResponseModel.successMsg();
    }

    @ApiOperation(value = "导入国际化数据", httpMethod = "POST", response = ResponseModel.class)
    @HibasePostRequestMapping(value = "/importMdsMessageSource")
    public ResponseModel importMdsMessageSource(@RequestParam("file") MultipartFile multipartFile,
                                                @RequestParam("country") String country,
                                                @RequestParam("type") String type) throws Exception{
        try {
            HibaseAssert.notNull(multipartFile.getInputStream(), "请上传文件");
        } catch (Exception e) {
            return ResponseModel.failMsg(2, "上传文件失败");
        }
        this.mdsMessageSourceService.importMdsMessageSource(multipartFile, country,type);
        return ResponseModel.successMsg();
    }
}
